/***********************************************************************************************************************
/*
/*  set_gamma.cpp
/*
/**********************************************************************************************************************/

#include "stdafx.h"
#include "math.h"

//======================================================================================================================
//  DATA
//======================================================================================================================

#if 0
double g_dGamma = 1.0;
int g_iBrightness = 50;
int g_iContrast = 50;
#endif


//======================================================================================================================
//  PROTOTYPES
//======================================================================================================================

WORD gamma_curve ( int i, double g );
bool set_gamma ( double dGamma );
WORD gamma_linear ( int i );


//======================================================================================================================
//  CODE
//======================================================================================================================

//-----------------------------------------------------------------------------
//   SET DEFAULT GAMMA
//-----------------------------------------------------------------------------
bool set_default_gamma ()
{
	HDC hdc = GetDC ( NULL );

	if ( ! hdc )
		return false;

	WORD a [3][256];

	for ( int i = 0; i < 256; i++ )
		a[0][i] = a[1][i] = a[2][i] = gamma_linear ( i );

	BOOL iResult = SetDeviceGammaRamp ( hdc, (void*) a );

	ReleaseDC ( NULL, hdc );

	return FALSE == iResult ? false : true;
}


//-----------------------------------------------------------------------------
//   SET GAMMA RAMP
//-----------------------------------------------------------------------------
bool set_gamma_ramp ( double dGamma, int iBright, int iContrast )
{
	HDC hdc = GetDC ( NULL );

	if ( ! hdc )
		return false;

	WORD a [3][256];

	//-------------------------------
	// GAMMA
	//-------------------------------

	int iLevel = 50;

	double dBright   = 1 + ( ( ( ( double ) iBright   - 50 ) / 100) * 65535 );
	double dContrast = 1 +   ( ( ( double ) iContrast - 50 ) / 100 );
	double dLevel    = 1 +   ( ( ( double ) iLevel    - 50 ) / 100 );

	for ( int i = 0; i < 256; i++ )
	{
		// a[0][i] = a[1][i] = a[2][i] = gamma_curve ( i, dGamma );

		double dVal = i * 256;
		dVal = ( pow ( dVal / 65535, 1 / dGamma ) * 65535 ) + 0.5;
		dVal = (((( dVal / 65535 ) - 0.5) * dContrast ) + 0.5) * 65535;
		dVal = dVal += dBright;
		dVal *= dLevel;

		a[0][i] = a[1][i] = a[2][i] = (WORD) min ( 65535, max ( 0, dVal ) );
	}

	BOOL iResult = SetDeviceGammaRamp ( hdc, (void*) a );

	ReleaseDC ( NULL, hdc );

	return FALSE == iResult ? false : true;
}


//-----------------------------------------------------------------------------
//   GAMMA
//-----------------------------------------------------------------------------
WORD gamma_curve ( int i, double g )
{
	return (WORD) min ( 65535, max ( 0, pow ( ( i + 1 ) / 256.0, g ) * 65535 + 0.5 ) );
}


//-----------------------------------------------------------------------------
//   GAMA LINEAR
//-----------------------------------------------------------------------------
WORD gamma_linear ( int i )
{
	return (WORD) ( i * 256 );
}


//-----------------------------------------------------------------------------
//   GET DEFAULT GAMMA
//-----------------------------------------------------------------------------
#if 0
void get_default_gamma ()
{
	HDC hdc = GetDC ( NULL );

	if ( ! hdc )
		return;

	// WORD s_aDefaultGamma [3][256];
	GetDeviceGammaRamp ( hdc, s_aDefaultGamma );

	ReleaseDC ( NULL, hdc );
}
#endif

